﻿
using DataCollection.Core.Models;
using DataCollection.Infrastructure;
using Mapster;

namespace DataCollection.WinForm.Forms
{
    public partial class ManageTemplateForm : DCBaseForm
    {
        #region 属性

        /// <summary>
        /// 当前页码
        /// </summary>
        private int PageNumber = 1;

        /// <summary>
        /// 每页数量
        /// </summary>
        private int PageSize = 10;

        /// <summary>
        /// 总数量
        /// </summary>
        private int TotalPages;

        /// <summary>
        /// 类型 0-模板管理 1-模板选择
        /// </summary>
        public int Type;

        #endregion

        #region 自定义方法
        /// <summary>
        /// 布局
        /// </summary>
        private void SetupForm()
        {
            ListTable.Columns = new AntdUI.ColumnCollection {
                new AntdUI.Column("Number","模板编号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("PipePosition","管道位置",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("DesignPressure","设计压力",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("FirstStage","第一阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("SecondStage","第二阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("ThirdStage","第三阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("IntensityStage","强度阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("RigorStage","严密阶段",AntdUI.ColumnAlign.Center)
            };
        }

        /// <summary>
        /// 列表
        /// </summary>
        public void LoadList()
        {
            Dictionary<string, object> where = new Dictionary<string, object>();
            List<TemplateModel> list = TemplateModel.ListPage(where, PageNumber, PageSize, ref TotalPages);
            List<TemplateItem> itemList = new List<TemplateItem>();
            foreach (TemplateModel model in list)
            {
                TemplateItem item = new TemplateItem();
                model.Adapt(item);
                itemList.Add(item);
            }
            ListTable.DataSource = itemList;
            Pagination1.PageSize = PageSize;
            Pagination1.Total = TotalPages;
        }

        #endregion

        #region 系统事件
        public ManageTemplateForm()
        {
            InitializeComponent();
            FullScreen = true;
        }

        private void TemplateManageForm_Load(object sender, EventArgs e)
        {
            if (this.Type == 1)
            {
                Header.Text = "选择试压模板";
            }
            else
            {
                Header.Text = "试压模板管理";
            }
            SetupForm();
            DCSpin(this, "加载数据...", (action) =>
            {
                LoadList();
            }, null, () => { });

        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            AddTemplateForm frm = new AddTemplateForm();
            frm.Owner = this;
            frm.ShowDialog();
        }

        private void Pagination1_ValueChanged(object sender, AntdUI.PagePageEventArgs e)
        {
            PageNumber = e.Current;
            Task.Run(() =>
            {
                LoadList();
            });
        }
       
        private void ListTable_CellDoubleClick(object sender, AntdUI.TableClickEventArgs e)
        {
            if (Type == 1)
            {
                if (e.Record is TemplateItem data)
                {
                    string msg = string.Format("您确定选择【{0}】模板吗？", data.Number);
                    AntdUI.Modal.open(new AntdUI.Modal.Config(this, "友情提示", msg)
                    {
                        OnOk = config =>
                        {
                            TemplateItem item = (TemplateItem)e.Record;
                            if (this.Owner is AddProjectForm)
                            {
                                AddProjectForm frm = (AddProjectForm)this.Owner;
                                frm.TemplateService = item;
                            }
                            this.Invoke(new MethodInvoker(delegate
                            {
                                this.Close();
                            }));
                            return true;
                        },
                    });
                }
            }
        }

        private void ManageTemplateForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.Owner is AddProjectForm)
            {
                AddProjectForm frm = (AddProjectForm)this.Owner;
                frm.LoadTemplateInfo();
            }
        }
        #endregion
    }
}
